package juc.day01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/*
写入并复制   CopyOnWriteArrayList
注意:
    添加操作多时，效率低，因为每完成一次写入操作就会进行一次复制，开销大
    并发操作迭代多时可以选择
 */
public class TestCopyOnWriteArrayList {

    public static void main(String[] args) {
        HelloThread h = new HelloThread();
        for (int i = 0; i < 10; i++) {
            new Thread(h).start();
        }
    }
}

class HelloThread implements Runnable {
    private static CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();

    static {
        list.add("AA");
        list.add("BB");
        list.add("CC");
    }

    @Override
    public void run() {
        Iterator<String> iter = list.iterator();
        while (iter.hasNext()) {
            System.out.println(Thread.currentThread().getName() + "\t" + iter.next());
        }
    }
}
